package com.egdigital.allzic.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;

public class RenderScriptUtils {

	public static Bitmap BlurImage(Bitmap input, Context context, int radius) {
		// create and alloc
		RenderScript rsScript = RenderScript.create(context);
		Allocation alloc = Allocation.createFromBitmap(rsScript, input);
		// init blur
		ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, alloc.getElement());
		blur.setRadius(radius);
		blur.setInput(alloc);
		// set blur
		Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig());
		Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);
		blur.forEach(outAlloc);
		// commit
		outAlloc.copyTo(result);
		// clean memory
		rsScript.destroy();
		return result;
	}
}
